<div class="tmd-doc">
<p></p>
<h1 class="tmd-header-1">
A channel comparison bug when channels are used as <code class="tmd-code-span">case</code> expressions of <code class="tmd-code-span">switch</code> code blocks (in Go toolchain v1.20 - v1.23.x)
</h1>
<p></p>
<div class="tmd-usual">
<span class="tmd-italic">(The releatd issue: </span><a href="https://github.com/golang/go/issues/67190"><span class="tmd-italic">https://github.com/golang/go/issues/67190</span></a><span class="tmd-italic">.)</span>
</div>
<p></p>
<div class="tmd-usual">
In Go toolchain v1.20 - v1.23.x, there is a channel value comparison bug, which is demonstrated in the following program:
</div>
<p></p>
<pre class="tmd-code">
<code class="language-Go">// chan-cmp-bug.go
package main

import "fmt"

func main() {
	x := make(chan struct{})

	var y &lt;-chan struct{} = x

	switch x {
	case y: fmt.Print(1)
	default: fmt.Print(0)
	}

	switch y {
	case x: fmt.Print(1)
	default: fmt.Print(0)
	}

	fmt.Println(x == y)
}
</code></pre>
<p></p>
<div class="tmd-usual">
The program should print <code class="tmd-code-span">11true</code>. But it prints <code class="tmd-code-span">01true</code> with Go toolchain versions from v1.20 to v1.23.0 (exclusive) <sup><a id="fn:gotv:ref-1" href="#fn:gotv">[1]</a></sup>.
</div>
<p></p>
<pre class="tmd-code">
$ gotv 1.19. run chan-cmp-bug.go
[Run]: $HOME/.cache/gotv/tag_go1.19.13/bin/go run chan-cmp-bug.go
11true
$ gotv 1.20 run chan-cmp-bug.go
[Run]: $HOME/.cache/gotv/tag_go1.20/bin/go run chan-cmp-bug.go
01true
$ gotv 1.21. run chan-cmp-bug.go
[Run]: $HOME/.cache/gotv/tag_go1.21.12/bin/go run chan-cmp-bug.go
01true
$ gotv 1.22. run chan-cmp-bug.go
[Run]: $HOME/.cache/gotv/tag_go1.22.5/bin/go run chan-cmp-bug.go
01true
$ gotv 1.23.0 run chan-cmp-bug.go
[Run]: $HOME/.cache/gotv/tag_go1.23rc1/bin/go run chan-cmp-bug.go
11true
</pre>
<p></p>
<div class="tmd-usual">
The bug started from Go toolchain version v1.20 and has been fixed since v1.23.0.
</div>
<p></p>
<div class="tmd-usual">
If you are using a Go toolchain version between v1.20 and v1.23.0 (exclusive), you should be aware of this bug.
</div>
<p></p>
<hr  class="tmd-seperator"/>
<p></p>

<ol class="tmd-list tmd-footnotes" id="fn:">
<li id="fn:gotv" class="tmd-list-item tmd-footnote-item">
<div id="gotv" class="tmd-usual">
GoTV (<a href="https://go101.org/apps-and-libs/gotv.html">https://go101.org/apps-and-libs/gotv.html</a>) is a tool used to install and use coexisting installations of Go toolchain verisons harmoniously.
</div>
<a href="#fn:gotv:ref-1">↩︎</a></li>
</ol>
</div>
